package com.example.lg.aliplaydemo470.activity;

import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.aliyun.player.IPlayer;
import com.aliyun.player.nativeclass.PlayerConfig;
import com.aliyun.player.source.UrlSource;
import com.aliyun.vodplayerview.constants.PlayParameter;
import com.aliyun.vodplayerview.widget.AliyunVodPlayerView;
import com.example.lg.aliplaydemo470.GlobalConfig;
import com.example.lg.aliplaydemo470.R;

import java.io.File;
import java.io.IOException;

/**
 * 试看功能 展示
 */
public class TrailersViewActivity extends AppCompatActivity {
    AliyunVodPlayerView mAliyunVodPlayerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trailers_view);


        initAliyunPlayerView();
        playVideo();//播放视频

        initEvent();
    }

    private void initEvent() {

        //开启试看，并且试看30秒
        mAliyunVodPlayerView.startTrailersView(true, 1 * 10 * 1000);

        mAliyunVodPlayerView.setOnVIPClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(TrailersViewActivity.this, "跳转到会员购买界面", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAliyunVodPlayerView.trailersClosed();
            }
        });






    }

    private void playVideo() {
        String videoPath = PlayParameter.PLAY_PARAM_URL;
       // String videoPath = "http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4";


        //下面的这 2 种视频路径，到4.7.0版本，不支持播放
        //String videoPath1 = "android.resource://" + getPackageName() + "/" + R.raw.video_1;
        // String videoPath2 = "file:///android_asset/video_1.mp4";

        UrlSource urlSource = new UrlSource();
        urlSource.setUri(videoPath);


        PlayerConfig playerConfig = mAliyunVodPlayerView.getPlayerConfig();
        //默认是5000
        int maxDelayTime = 5000;
        if (PlayParameter.PLAY_PARAM_URL.startsWith("artp")) {
            //如果url的开头是artp，将直播延迟设置成100，
            maxDelayTime = 100;
        }
        playerConfig.mMaxDelayTime = maxDelayTime;



        mAliyunVodPlayerView.setPlayerConfig(playerConfig);
        mAliyunVodPlayerView.setLocalSource(urlSource);


    }

    private void initAliyunPlayerView() {

        String sdDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test_save_cache";
        File file = new File(sdDir);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mAliyunVodPlayerView = findViewById(R.id.aliyunVodPlayerView);
        //保持屏幕敞亮
        mAliyunVodPlayerView.setKeepScreenOn(true);
        PlayParameter.PLAY_PARAM_URL = GlobalConfig.DEFAULT_URL;
        mAliyunVodPlayerView.setPlayingCache(false, sdDir, 60 * 60 /*时长, s */, 300 /*大小，MB*/);
        mAliyunVodPlayerView.setTheme(AliyunVodPlayerView.Theme.Blue);
        mAliyunVodPlayerView.setCirclePlay(true);//是否循环播放
        mAliyunVodPlayerView.setAutoPlay(false);//是否自动播放
        //设置画面缩放模式：宽高比填充，宽高比适应，拉伸填充
        //mAliyunVodPlayerView.setVideoScalingMode(IPlayer.ScaleMode.SCALE_ASPECT_FILL);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (mAliyunVodPlayerView != null) {
            mAliyunVodPlayerView.setAutoPlay(false);
            mAliyunVodPlayerView.onResume();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAliyunVodPlayerView != null) {
            mAliyunVodPlayerView.setAutoPlay(false);
            mAliyunVodPlayerView.onStop();
        }
    }
}
